package zsc.liangyi.blog.web.client;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.glassfish.gmbal.ParameterNames;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import zsc.liangyi.blog.model.domain.Article;
import zsc.liangyi.blog.model.domain.Comment;
import zsc.liangyi.blog.model.domain.User;
import zsc.liangyi.blog.service.*;
import zsc.liangyi.blog.service.impl.CommentServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
public class IndexController {
    //日志
    private static final Logger logger= LoggerFactory.getLogger(IndexController.class);
    @Autowired
    private ArticleService articleService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private SiteService siteService;
    @Autowired
    private UserService userService;
    @Autowired
    private CategoryService categoryService;
    //博客首页，自动跳转到文章页
    @GetMapping("/")
    private String index(Model model){
        return this.index(model,1,5);
    }

    //跳转到文章页
    @GetMapping("/page/{p}")
    public String index(Model model,
                        @PathVariable("p") int page,
                        @RequestParam(value = "count",defaultValue = "5") int count){
        //获取文章分页全部
        PageInfo<Article> articles=articleService.selectArticleWithPage(page,count);
        //获取文章热度信息
        List<Article> articleList_heat=articleService.getHeatArticle();
        model.addAttribute("articles",articles);
        model.addAttribute("articleList_heat",articleList_heat);
        logger.info("logger.info::首页-分页获取文章信息：页码："+page+" ，条数："+count);
        log.info("log.info::首页-分页获取文章信息：页码："+page+" ，条数："+count);
        return "client/index";
    }

    //搜索文章
    @GetMapping("/search")
    public String index(Model model,
                        @RequestParam(value = "p",defaultValue = "1") int page,
                        @RequestParam(value = "count",defaultValue = "5") int count,
                        @RequestParam("keyword") String keyword){
        //获取文章分页全部
        PageInfo<Article> articles=articleService.selectArticleWithKeyWord(page,count,keyword);
        model.addAttribute("articles",articles);
        logger.info("logger.info::搜索：页码："+page+" ，条数："+count+" ，关键词："+keyword);
        log.info("log.info::搜索-分页获取文章信息：页码："+page+" ，条数："+count+" ，关键词："+keyword);
        return "client/search_result";
    }

    //用户详情界面
    @GetMapping("/user/{username}")
    public String userinfo(Model model,
                           @PathVariable String username){
        Map<String,Object> userInfo=new HashMap<String,Object>();
        //获取用户信息
        User user=userService.getByUserName(username);
        //设置隐私字段为空
        user.setPassword(null);
        userInfo.put("user",user);
        //获取该用户所属文章
        List<Article> articleList=articleService.selectArticleWithColumn("user_id",user.getId());
        userInfo.put("articleList",articleList);
        //封装
        model.addAttribute("userinfo",userInfo);
        return "client/userinfo";
    }

    //根据分类id返回文章
    @GetMapping("/category")
    public String cate(@RequestParam(value = "cid") Integer cid,
                       Model model){
        List<Article> articleList=articleService.selectArticleWithColumn("category_id",cid);
        model.addAttribute("articleList",articleList);
        model.addAttribute("category",categoryService.queryById(cid));
        return "client/category";
    }

    //点击文章跳转到文章详情页
    @GetMapping("/article/{id}")
    public String articleDetails(Model model,
                                 @PathVariable("id") Integer id){
        Article article=articleService.selectArticleWithId(id);
        User author=userService.selectUserWithId(article.getUserId());
        article.setUser(author);
        System.out.println("文章详情："+article);
        if(article!=null){
            //查询封装article信息
            getArticleComments(model,article);
            //更新文章点击量
            siteService.updateStatistics(article);
            model.addAttribute("article",article);
            return "client/articleDetails";
        }else{
            log.warn("查询文章结果为空，查询文章id="+id);
            return "comm/error_404";
        }
    }
    //查询文章的评论信息，并补充到文章详情里面
    private void getArticleComments(Model model,Article article){
        //如果该文章允许评论
        if(article.getAllowComment()){
            String cp= (String) model.getAttribute("cp");
            cp= StringUtils.isBlank(cp)?"1":cp;
            model.addAttribute("cp",cp);
            PageInfo<Comment> commentPageInfo=commentService.getComments(article.getId(),Integer.parseInt(cp),6);
            model.addAttribute("cp",cp);
            model.addAttribute("comments",commentPageInfo);
        }
    }
}
